package Application;

import java.util.ArrayList;
import java.util.List;

import VectorApproach.TrajVector;
import XmlLoader.FGObjProcess;
import XmlLoader.FGObjectTrajectoryStructure;
import XmlLoader.Position;
import Cluster.Trajectory;
import Cluster.Vector;
import MyLoader.MyLoader;

public class DirClassfication {
	
	public static ArrayList<ArrayList<FGObjectTrajectoryStructure>> ACT001_dirCluster(List<FGObjectTrajectoryStructure> list, int angleRange){
		ArrayList<ArrayList<FGObjectTrajectoryStructure>> listDirBulk = new ArrayList<ArrayList<FGObjectTrajectoryStructure>>();
		int index = 0;
		while(index < 360/angleRange) {
			listDirBulk.add(new ArrayList<FGObjectTrajectoryStructure>());
			index = index + 1;
		}
		
		index = 0;
		Position posFirst = null, posLast = null;
		Vector vStandard = new Vector(0, 0, 0, 1);
		Vector vect = null;
		double angle = 0.0;
		while(index < list.size()) {
			posFirst = list.get(index).GET003_elementIn_list_fGObject(0).GET001_centerReal();
			posLast = list.get(index).GET003_elementIn_list_fGObject(list.get(index).GET002_nNumberOfPoint() - 1).GET001_centerReal();
			vect = new Vector(posFirst.x, posFirst.y, posLast.x - posFirst.x, posLast.y - posFirst.y);
			angle = vStandard.ACT001_countAngle(vect) % 360;
			listDirBulk.get((int) (angle / angleRange)).add(list.get(index));
			index = index + 1;
		}
		return listDirBulk;
	}
	
	public static ArrayList<ArrayList<TrajVector>> ACT002_dirVCluster(List<TrajVector> list, int angleRange){
		ArrayList<ArrayList<TrajVector>> listDirBulk = new ArrayList<ArrayList<TrajVector>>();
		int index = 0;
		while(index < 360/angleRange) {
			listDirBulk.add(new ArrayList<TrajVector>());
			index = index + 1;
		}
		
		index = 0;
		Vector vStandard = new Vector(0, 0, 0, 1);
		double angle = 0.0;
		while(index < list.size()) {
			angle = vStandard.ACT001_countAngle(list.get(index)) % 360;
			listDirBulk.get((int) (angle / angleRange)).add(list.get(index));
			index = index + 1;
		}
		return listDirBulk;
	}

	public static void main(String[] args) {
		//generate list trajectory index
		System.out.println("Generate trajectory index file");
		new ReadFileGenerateor().ACT001_genFile(args[0], Integer.parseInt(args[1]), Integer.parseInt(args[2]));
						
		//load trajectory xml data
		System.out.println("Load trajectory xml file");
		FGObjProcess fgObj = new FGObjProcess(args[3]);
		List<String> list_objIndex = fgObj.ACT004_getStringList(args[0]);
		List<FGObjectTrajectoryStructure> list = new MyLoader().ACT001_getListOfObj(args[3], list_objIndex);
		
		ArrayList<ArrayList<FGObjectTrajectoryStructure>> listDirCluster = DirClassfication.ACT001_dirCluster(list, 90);
		int index = 0;
		while(index < listDirCluster.size()) {
			ArrayList<FGObjectTrajectoryStructure> listOne = listDirCluster.get(index);
			if(listOne.size() > 0) {
				ImageGenerator.GenTraj(args[4] + "/" + index + ".png", listOne, Integer.parseInt(args[5]), Integer.parseInt(args[6]), args[7]);
			}
			index= index + 1;
		}
	}
}
